import java.awt.GridLayout;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

/**
 * AddClubWindow.java
 * 
 * Provides a window to add a club to the database
 * 
 * @author Steven Glazer
 * @author Kyle Marcotte
 * @author Sam Milton
 */
public class AddClubWindow extends JDialog implements ActionListener {
	
	private static final long serialVersionUID = -1987273009263189086L;
	
	// Class Variables
	private final int HGAP = 12;
	private final int VGAP = 12;
	
	private AddClubWindow self = this;
	private JPanel pane;
	private JPanel inputPane;
	private JPanel buttonPane;
	private JButton selectBut;
	private JButton cancelBut;
	private JTextField nameInput;
	private JTextField rankInput;
	private JTextField typeInput;
	
	private boolean added = false;
	
	/**
	 * Constructor
	 */
	public AddClubWindow(JFrame parent, boolean modal) {
		super(parent, modal);
		this.setTitle("Add Club");
		pane = (JPanel)this.getContentPane();
		pane.setLayout(new BorderLayout(HGAP, VGAP));
		pane.setBorder(BorderFactory.createEmptyBorder(VGAP, HGAP, VGAP, HGAP));
		
		inputPane = new JPanel();
		inputPane.setLayout(new GridLayout(3,1, HGAP, VGAP));
		buttonPane = new JPanel();
		buttonPane.setLayout(new GridLayout(1, 2, HGAP, VGAP));
		
		nameInput = new JTextField("Name", 20);
		rankInput = new JTextField("Rank");
		typeInput = new JTextField("Type");
		
		inputPane.add(nameInput);
		inputPane.add(rankInput);
		inputPane.add(typeInput);
		
		selectBut = new JButton("Add");
		selectBut.addActionListener(this);
		cancelBut = new JButton("Cancel");
		cancelBut.addActionListener(this);
		
		buttonPane.add(selectBut);
		buttonPane.add(cancelBut);
		
		pane.add(inputPane, BorderLayout.CENTER);
		pane.add(buttonPane, BorderLayout.SOUTH);
		
		this.pack();
		this.setLocationRelativeTo(parent);
		this.setVisible(true);
	}
	
	/**
	 * actionPerformed method for ActionListener
	 * Used for select and cancel buttons
	 */
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == selectBut && nameInput.getText().length() > 0 &&
				rankInput.getText().length() > 0 && typeInput.getText().length() > 0 &&
				!nameInput.getText().equals("Name") && !rankInput.getText().equals("Rank") &&
				!typeInput.getText().equals("Type")) {
			added = true;
			this.setVisible(false);
		}
		else if (e.getSource() == selectBut)
			JOptionPane.showMessageDialog(self, "Fields Must Have Valid Input", 
					"Club Addition Warning", JOptionPane.WARNING_MESSAGE);
		else if (e.getSource() == cancelBut)
			this.setVisible(false);
	}
	
	/**
	 * Returns the new Club information
	 * 
	 * @return	An Array of the new Club information
	 */
	public String[] getResult() {
		if(added) {
			String[] result = new String[3];
			result[0] = nameInput.getText();
			result[1] = rankInput.getText();
			result[2] = typeInput.getText();
			return result;
		}
		else 
			return null;
	}
}
